#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class VectorVectorIterator {
public:
    VectorVectorIterator (vector<vector<int> > input): myVec(input) {
        row = myVec.begin();
        if (row != myVec.end()) col = (*row).begin(); //detect empty row
        //move to a valid point
        moveCursor();
    }

    void moveCursor() {
        while (row != myVec.end() && col == (*row).end()) {
            ++row;
            if (row != myVec.end()) {
                col = (*row).begin();    
            }
        }
    }

    bool hasNext() {
        return row != myVec.end();
    }

    int next() {
        int t = *col++;
        moveCursor();
        return t;
    }

private:
    vector<vector<int> >myVec;
    vector<vector<int> >::iterator row;
    vector<int>::iterator col;
};


int main(int argc, char **argv)
{
    cout << "-----------Test 1----------" << endl;
    vector<vector<int> > test1;
    VectorVectorIterator testIter(test1);
    while (testIter.hasNext()) {
        cout << testIter.next() << " "; 
    }
    cout << endl;

    cout << "-----------Test 2----------" << endl;
    vector<vector<int> > test2;
    vector<int> a1(3, 0);;
    a1[0] = 0; a1[1] = 1; a1[2] = 2; 
    vector<int> a2(2, 0);
    a2[0] = 3; a2[1] = 4;
    vector<int> a3(3, 0);
    a3[0] = 5; a3[1] = 6; a3[2] = 7;
    test2.push_back(a1);
    test2.push_back(a2);
    test2.push_back(a3);
    VectorVectorIterator testIter2(test2);
    while (testIter2.hasNext()) {
        cout << testIter2.next() << " "; 
    }
    cout << endl << endl;

    cout << "-----------Test 3----------" << endl;
    vector<vector<int> > test3;
    vector<int> a11(3, 0);
    a11[0] = 0; a11[1] = 1; a11[2] = 2; 
    vector<int> a21(2, 0);
    a21[0] = 3; a21[1] = 4;
    vector<int> a31(3, 0);
    a31[0] = 5; a31[1] = 6; a31[2] = 7;
    vector<int> a41;
    test3.push_back(a11);
    test3.push_back(a21);
    test3.push_back(a41);
    test3.push_back(a31);
    VectorVectorIterator testIter3(test3);
    while (testIter3.hasNext()) {
        cout << testIter3.next() << " "; 
    }
    cout << endl << endl;

}
